package com.csl.seckill.controller;

import com.csl.seckill.commom.Result;
import com.csl.seckill.entity.UserDo;
import com.csl.seckill.service.UserService;
import com.csl.seckill.vo.UserDetailVo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/v1/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据id查询用户信息，当查询失败，走hystrixGet的备选代码
     * @param id
     * @return
     */
    @HystrixCommand(fallbackMethod = "hystrixGet")
    @GetMapping(value = "/{id}")
    public UserDo getUserById(@PathVariable(name = "id")Long id){
        UserDo userDo =userService.getUserById(id);
        if (null== userDo){
            throw new RuntimeException("不存在该用户，或者该用户信息无法被找到");
        }
        return userDo;
    }

    @PostMapping(value = "/login")
    public String login(){

        return "login";

    }


    public UserDo hystrixGet(@PathVariable("id")Long id){
        return new UserDo().setUserId(id)
                .setUserName("无对应信息");
    }

}
